package com.zhny.sensor.nio;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;

import java.net.InetSocketAddress;

/**
 * @Auther: zhy
 * @Date: 2019/11/14
 * @Description:
 */
public class BCMNettyClient {

    public static void main(String[] args) throws InterruptedException {
        Bootstrap bootstrap = new Bootstrap();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        bootstrap.group(workerGroup)
                .channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, true)
                .option(ChannelOption.SO_KEEPALIVE, true)
//                .remoteAddress(new InetSocketAddress("119.23.218.131",2021))//  106.75.132.85
//                .remoteAddress(new InetSocketAddress("127.0.0.1",2021))//
                .remoteAddress(new InetSocketAddress("106.75.154.40",2021))
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline()
                                .addLast(new DelimiterBasedFrameDecoder(2048,
                                        Unpooled.copiedBuffer(new byte[]{(byte) 0xFC,(byte) 0xFC})))
                                .addLast(new ClientHandler() );
                    }
                });
        //连接服务器
        ChannelFuture channelFuture = bootstrap.connect().sync();
        System.out.println("客户端已连接服务器....");
        channelFuture.channel().closeFuture().sync();
        long end = System.currentTimeMillis();
        System.out.println("结束时间：" + end);
    }

}
